package com.gobang.gobang;

import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Room {
    private final String roomId;
    private final String roomName;
    private Socket player1;
    private Socket player2;
    private String player1Name;
    private String player2Name;
    private boolean isGameStarted;
    private int player1Score = 0;
    private int player2Score = 0;
    
    public Room(String roomId, String roomName, Socket creator, String creatorName) {
        this.roomId = roomId;
        this.roomName = roomName;
        this.player1 = creator;
        this.player1Name = creatorName;
        this.isGameStarted = false;
        this.player1Score = 0;
        this.player2Score = 0;
    }
    
    public boolean isFull() {
        return player1 != null && player2 != null;
    }
    
    public boolean addPlayer(Socket player, String playerName) {
        if (player1 == null) {
            player1 = player;
            player1Name = playerName;
            return true;
        } else if (player2 == null) {
            player2 = player;
            player2Name = playerName;
            return true;
        }
        return false;
    }
    
    public void removePlayer(Socket player) {
        if (player.equals(player1)) {
            player1 = null;
            player1Name = null;
            if (player2 != null) {
                player1 = player2;
                player1Name = player2Name;
                player1Score = player2Score;
                player2 = null;
                player2Name = null;
                player2Score = 0;
            }
        } else if (player.equals(player2)) {
            player2 = null;
            player2Name = null;
            player2Score = 0;
        }
        isGameStarted = false;
    }
    
    public boolean canJoin() {
        return (player1 == null || player2 == null) && !isGameStarted;
    }
    
    // Getters
    public String getRoomId() { return roomId; }
    public String getRoomName() { return roomName; }
    public Socket getPlayer1() { return player1; }
    public Socket getPlayer2() { return player2; }
    public String getPlayer1Name() { return player1Name; }
    public String getPlayer2Name() { return player2Name; }
    public boolean isGameStarted() { return isGameStarted; }
    
    public void setGameStarted(boolean started) {
        this.isGameStarted = started;
        if (started) {
            player1Score = 0;
            player2Score = 0;
        }
    }
    
    public void incrementScore(Socket winner) {
        if (winner.equals(player1)) {
            player1Score++;
        } else if (winner.equals(player2)) {
            player2Score++;
        }
    }
    
    public String getScoreInfo() {
        return player1Score + "-" + player2Score;
    }
    
    public String getStatus() {
        if (isGameStarted) {
            return "对局中";
        } else if (player2 == null) {
            return "等待加入";
        } else {
            return "准备开始";
        }
    }
} 